﻿using ObjectBakery;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Data;
using System.Collections;
using ObjectBakery.Tests.TestDoubles;
using ObjectBakery.Tests.TestFactories;
using System.Collections.Generic;
using System.Reflection;
using ObjectBakery.Adapters;
using System.Linq;

namespace ObjectBakery.Tests
{
    
    
    /// <summary>
    ///This is a test class for ObjectAdapterTest and is intended
    ///to contain all ObjectAdapterTest Unit Tests
    ///</summary>
    [TestClass()]
    public class ObjectAdapterTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod()]
        public void FillTest()
        {
            FillTestHelper<Product>();
        }

        public void FillTestHelper<T>()
        {
            
            DataTable table = new TestTableFactory().BuildProducts();
            IList list = new List<T>();

            var factory = new AdapterFactory<DataTable, IList>();
            var adapter = factory.GetAdapter<T>(table);
            adapter.Fill(list);

            Assert.IsTrue(list.Count.Equals(table.Rows.Count));
            Assert.IsTrue(list[0].GetType() == typeof(T));

            //-- retest type unknown in advance
            Type type = typeof(T);
            var adapter2 = factory.GetAdapter(table, type);
            IList list2 = new List<T>();
            adapter2.Fill(list2);

            Assert.IsTrue(list2.Count.Equals(table.Rows.Count));
            Assert.IsTrue(list2[0].GetType() == typeof(T));

        }

        [TestMethod()]
        public void CanBakeObjectsFromDataTable()
        {
            var adapter = new AdapterFactory<DataTable, IList>().GetAdapter<Product>(new TestTableFactory().BuildProducts());
            List<Product> products = new List<Product>();
            adapter.Fill(products);
            Assert.IsTrue(products.Count.Equals(adapter.ImplementationObject.Rows.Count));
        }
        /// <summary>
        ///A test for Flush
        ///</summary>
        [TestMethod()]
        public void FlushTest()
        {
            var adapter = new AdapterFactory<DataTable, IList>().GetAdapter(new DataTable(), typeof(Product));

            adapter.ImplementationObject.Rows.Clear(); //ensure 0 rows
            IList list = new TestProductFactory().Build();
            adapter.Flush(list);
            Assert.IsTrue(list.Count.Equals(adapter.ImplementationObject.Rows.Count));
        }

        [TestMethod]
        public void CanFillInReverseDirections()
        {
            //IList -> DataTable 
            var bridge1 = new AdapterFactory<IList, DataTable>(); //direction 
            
            //setup
            var products1 = new TestProductFactory().Build();
            var adapter1 = bridge1.GetAdapter(products1, products1[0].GetType());
            var table1 = new DataTable();
            
            //act
            adapter1.Fill(table1);
            
            Assert.IsTrue(table1.Rows.Count > 0);
            Assert.IsTrue(table1.Rows.Count == products1.Count);
           

            //DataTable -> IList
            var bridge2 = new AdapterFactory<DataTable, IList>(); //reverse direction 
            
            //setup
            var table2 = new TestTableFactory().BuildProducts();
            var adapter2 = bridge2.GetAdapter(table2, typeof(Product));
            var products2 = new List<Product>();

            //act
            adapter2.Fill(products2);

            Assert.IsTrue(products2.Count > 0);
            Assert.IsTrue(products2.Count == table2.Rows.Count);
        }
    }
}
